<?php
/**
 * Date: 10/22/12
 * Time: 10:36 PM
 * User: Amitav Roy
 */
class Global_model extends CI_Model {
	// Table where terms are stored.
	protected $term_table = 'term';

	// Table where categories are stored.
	protected $category_table = 'category';

	// Description table name.
	protected $description_table = 'description';

	// Term and entity id
	protected $term_entity_table = 'term_entity';

	// Billers table
	protected $billers_table = 'billers';

	function __construct() {
		parent::__construct();
	}

	/**
	 * This function will return an array of the categories
	 * If entity type is provided, then the result will be
	 * entity specific.
	 *
	 * @param string $entity_type
	 * @return array
	 */
	function global_get_categories($entity_type = NULL) {
		$this->db->select('ct.name,ct.cid');
		$this->db->from($this->category_table . ' ct');

		if ($entity_type != NULL)
			$this->db->where('entity',$entity_type,'=');

		$this->db->order_by('ct.name','ASC');

		$query = $this->db->get();

		// print $this->db->last_query();

		$result = $query->result_array();

		return $result;
	}

	/**
	 * This is the function to get the terms in the system,
	 * if category_id is provided, then it will show terms
	 * under that category.
	 *
	 * @param int $category_id
	 * @return array of terms
	 */
	function global_get_term($category_id = NULL) {
		$this->db->select('t.tid, t.name');
		$this->db->from($this->term_table . ' t');

		if ($category_id != NULL)
			$this->db->where('c.cid',$category_id,'=');

		$this->db->join($this->category_table . ' c','c.cid=t.cid');

		$query = $this->db->get();
		$result = $query->result_array();

		return $result;
	}

	/**
	 * This function is to add the description for entity.
	 *
	 * @param $description
	 * @param $eid
	 */
	function global_add_description($description, $eid, $entity_type) {
		if (isset($description) && isset($eid)) {
			$data['description'] = $description;
			$data['eid'] = $eid;
			$data['entity_type'] = $entity_type;
			$this->db->insert($this->description_table,$data);
		}
	}

	/**
	 * This function is to add terms for an enity
	 * to the term_entity table.
	 *
	 * @param $tid
	 * @param $eid
	 */
	function global_add_term($tid, $eid) {
		$data['tid'] = $tid;
		$data['eid'] = $eid;

		$this->db->insert($this->term_entity_table,$data);
	}

	function get_dealers_dropdown() {
		$this->db->select();
		$this->db->from($this->billers_table);
		$this->db->order_by('name');

		$query = $this->db->get();

		return $query->result_array();
	}
}